<%@ page language="java" pageEncoding="UTF-8"%>
<%@page import="com.shsh.safemanage.dao.TitlePostManageGroupDao"%>
<%@page import="java.util.Calendar"%>
<%@page import="com.shsh.safemanage.dao.DepartmentDao"%>
<%@page import="com.shsh.safemanage.model.Department1"%>
<%@page import="com.shsh.safemanage.model.ProblemTransfer"%>
<%@page import="com.shsh.safemanage.model.ProblemTransferFeedback"%>
<%@page import="java.util.List"%>
<%@page import="com.shsh.safemanage.model.Problem"%>
<%@page import="com.shsh.safemanage.model.User"%>
<%@page import="com.shsh.safemanage.model.Role"%>
<%@page import="com.shsh.safemanage.model.JobPost"%>
<%@page import="com.shsh.safemanage.model.Shutdown"%>
<%@page import="com.shsh.safemanage.model.ShutdownType"%>
<%@page import="com.shsh.safemanage.dao.DaoCreator"%>
<%@page import="com.shsh.safemanage.dao.ProblemDao"%>
<%@page import="com.shsh.safemanage.action.ActionCreator"%>
<%@page import="com.shsh.safemanage.action.WriteProblemAction"%>
<%@page import="com.shsh.safemanage.action.UserAction"%>
<%@page import="com.shsh.safemanage.util.Constants"%>
<%@page import="com.shsh.safemanage.util.Util"%>
<%@page import="com.shsh.safemanage.util.RequestUtil"%>

<%
session.removeAttribute(Constants.SESSION_ESCALATE_USER);
session.removeAttribute(Constants.SESSION_MANAGE_GROUP_USER);
session.removeAttribute(Constants.SESSION_PROBLEM_DEPARTMENT1);

Integer problemType = RequestUtil.getParameter(request, Constants.REQUEST_PARAMETER_PROBLEM_TYPE, (Integer)null);
String problemName = Problem.getProblemName(problemType);

String problemIDStr = request.getParameter(Constants.REQUEST_PARAMETER_ID);
Long problemID = 0L;
try {
	problemID = new Long(problemIDStr);
} catch (Exception e) {
}

String transferIDStr = request.getParameter(Constants.REQUEST_PARAMETER_TRANSFER_ID);
Long transferID = 0L;
try {
	transferID = new Long(transferIDStr);
} catch (Exception e) {
}

DepartmentDao departmentDao = DaoCreator.createDepartmentDao();
ProblemDao problemDao = DaoCreator.createProblemDao();

String fromPage = request.getParameter(Constants.REQUEST_PARAMETER_FROM_PAGE);


TitlePostManageGroupDao titlePostManageGroupDao = DaoCreator.createTitlePostManageGroupDao();
WriteProblemAction writeProblemAction = ActionCreator.createWriteProblemAction();
UserAction userAction = ActionCreator.createUserAction();

Problem problem = problemDao.getProblemByID(problemID);
List<ProblemTransfer> transferList = problemDao.getProblemTransferByProblemID(problem.getId());
List<ShutdownType> shutdownTypeList = problemDao.getShutdownTypeList(false);

String initDateEndStr = Util.formatDate(problem.getWriteTime());

User user = (User) session.getAttribute(Constants.SESSION_USER);
JobPost post1 = titlePostManageGroupDao.getJobPostByID(user.getJobPost1ID());
JobPost post2 = titlePostManageGroupDao.getJobPostByID(user.getJobPost2ID());
JobPost post3 = titlePostManageGroupDao.getJobPostByID(user.getJobPost3ID());
Role theRole = userAction.getUserRole(request);

int role = writeProblemAction.calRole(post1, post2, post3, theRole);

String zhenggaiCuoshi = problem.getProblemZhenggai();
if(zhenggaiCuoshi == null && zhenggaiCuoshi.trim().length()==0){
	zhenggaiCuoshi = "";
}else {
	zhenggaiCuoshi = zhenggaiCuoshi.trim();
}
for (ProblemTransfer transfer : transferList) {
	List<ProblemTransferFeedback> feedBackList = transfer.getFeedbackList();
	for(ProblemTransferFeedback feedback:feedBackList) {
		String feedbackStr = feedback.getFeedback();
		if(feedbackStr != null && feedbackStr.trim().length()>0){
			zhenggaiCuoshi += ("\r\n" + feedbackStr.trim());
		}
	}
}


ProblemTransfer currentTransfer = problemDao.getProblemTransfer(problem.getWriteUserID(), problemID, user.getId());
int[] currentTime = Util.getCurrentDate();
int currentMonthEnd = Util.getCurrentMonthEnd();
%>
<div class="hider" id="hider">
	<span>&nbsp;</span>
</div>
<div class="position">
	<img src="images/position2.jpg" style="padding: 7px 9px; float: left;" />当前位置：上海高桥分公司问题管理平台 &gt;&gt; <%=problemName %>填报
</div>

<div class="tt">《高桥石化公司生产装置<%=problemName %>报告》</div>
<div class="content">
	<table align="center" border="1" width="99%" cellpadding="0"
		cellspacing="0">
		<tr>
			<td class="td1" colspan="2">
				<span style="margin-left: 30px; font-weight: bold">作业部（业务中心）：</span> <span><%=problem.getDepartment1Name()%></span>
				<span style="margin-left: 30px; font-weight: bold">装置：</span> <span><%=problem.getEquipmentName()%></span>
				<span style="margin-left: 30px; font-weight: bold"><%=problemName %>性质：</span>
				<span>
					<select id="shutdownType">
						<option value="0" selected="selected">请选择停工性质</option>
					<%for (ShutdownType shutdownType : shutdownTypeList) {%>
						<option value="<%=shutdownType.getId()%>"><%=shutdownType.getShutdownType()%></option>
					<%} %>
					</select>
				</span>
<%
if(problemType != null && problemType == Constants.PROBLEM_TYPE_NOTPLANED_SHUTDOWN) {
%>
				<span style="margin-left: 30px; font-weight: bold"><input type="checkbox" id="escalateZongbu" name="escalateZongbu" value="true"/>
				<label for="escalateZongbu">上报总部</label>
				</span>
<%
}
%> 
			</td>
		</tr>
	<tr>
			<td class="td1" width="60%">	
			<table width="100%">
			<tr>
				<td rowspan="2">
				<span style="margin-left: 30px; font-weight: bold"><%=problemName %>时间段：</span> 
				</td>
				<td>		
				<span style="margin-left: 10px; ">						
						<select id="startYear" onchange="refreshPeriodStart(true)">
							<%
							for(int i=2001;i<=2050;i++){
							%>
							<option value="<%=i %>" 
								<%
								if(i==currentTime[0]){
								%>
								selected="selected"
								<% 
								}
								%>								
								><%=i %></option>
							<%
							}
							%>
						</select>
						年						
						<select id="startMonth" onchange="refreshPeriodStart(true)">
							<%
							for(int i=1;i<=12;i++){
							%>
							<option value="<%=i %>" 
								<%
								if(i==currentTime[1]){
								%>
								selected="selected"
								<% 
								}
								%>								
								><%=i %></option>
							<%
							}
							%>
						</select>
						月				
						<select id="startDay" onchange="refreshPeriodStart(false)">
							<%
							for(int i=1;i<=currentMonthEnd;i++){
							%>
							<option value="<%=i %>" 
								<%
								if(i==currentTime[2]){
								%>
								selected="selected"
								<% 
								}
								%>								
								><%=i %></option>
							<%
							}
							%>
						</select>
						日		
						<select id="startHour" onchange="refreshPeriodStart(false)">
							<%
							for(int i=0;i<=23;i++){
							%>
							<option value="<%=i %>" 
								<%
								if(i==currentTime[3]){
								%>
								selected="selected"
								<% 
								}
								%>								
								><%=i %></option>
							<%
							}
							%>
						</select>
						时
						<select id="startMinute" onchange="refreshPeriodStart(false)">
							<%
							for(int i=0;i<=59;i++){
							%>
							<option value="<%=i %>" 
								<%
								if(i==currentTime[4]){
								%>
								selected="selected"
								<% 
								}
								%>								
								><%=i %></option>
							<%
							}
							%>
						</select>
						分
				</span>	
				</td>
			</tr>
			<tr>
			<td>
				<span style="margin-left: 10px; ">						
						<select id="endYear" onchange="refreshPeriodEnd(true)">
							<%
							for(int i=2001;i<=2050;i++){
							%>
							<option value="<%=i %>" 
								<%
								if(i==currentTime[0]){
								%>
								selected="selected"
								<% 
								}
								%>								
								><%=i %></option>
							<%
							}
							%>
						</select>
						年						
						<select id="endMonth" onchange="refreshPeriodEnd(true)">
							<%
							for(int i=1;i<=12;i++){
							%>
							<option value="<%=i %>" 
								<%
								if(i==currentTime[1]){
								%>
								selected="selected"
								<% 
								}
								%>								
								><%=i %></option>
							<%
							}
							%>
						</select>
						月				
						<select id="endDay" onchange="refreshPeriodEnd(false)">
							<%
							for(int i=1;i<=currentMonthEnd;i++){
							%>
							<option value="<%=i %>" 
								<%
								if(i==currentTime[2]){
								%>
								selected="selected"
								<% 
								}
								%>								
								><%=i %></option>
							<%
							}
							%>
						</select>
						日		
						<select id="endHour" onchange="refreshPeriodEnd(false)">
							<%
							for(int i=0;i<=23;i++){
							%>
							<option value="<%=i %>" 
								<%
								if(i==currentTime[3]){
								%>
								selected="selected"
								<% 
								}
								%>								
								><%=i %></option>
							<%
							}
							%>
						</select>
						时
						<select id="endMinute" onchange="refreshPeriodEnd(false)">
							<%
							for(int i=0;i<=59;i++){
							%>
							<option value="<%=i %>" 
								<%
								if(i==currentTime[4]){
								%>
								selected="selected"
								<% 
								}
								%>								
								><%=i %></option>
							<%
							}
							%>
						</select>
						分
				</span>	
			</td>
			</tr>
			</table>
			</td>
			<td  class="td1" width="40%">
				<span style="margin-left: 30px; font-weight: bold"><%=problemName %>时间：</span> 
				共<input  style="margin-left: 0px; width:50px;" readonly="readonly" type="text" id="periodMinutes" value="0"/>分钟
				<br/>
				<input  style="margin-left: 30px; width:35px;" readonly="readonly" type="text" id="periodDay"value="0"/>天
				<input  style="margin-left: 5px; width:25px;" readonly="readonly" type="text" id="periodHour" value="0"/>小时
				<input  style="margin-left: 5px; width:25px;" readonly="readonly" type="text" id="periodMinute" value="0"/>分钟
			</td>
	</tr>
	<tr>
			<td class="td1" colspan="2">			
				<span style="margin-left: 30px; font-weight: bold"><%=problemName %>名称：</span>  <br/>
				<span style="margin-left: 30px; ">
						<textarea rows="3" style="width:90%;" id="problemTitle"><%=problem.getProblemTitle()%></textarea>	
				</span>	
			</td>
	</tr>
	<tr>
		<td class="td1" colspan="2"><span
			style="margin-left: 30px; font-weight: bold">经过及处理过程：</span> <br/>
			<span style="margin-left: 30px; ">
			<textarea rows="5" style="width:90%;" id="problemContent" style="text-align:left;"><%=problem.getProblemContent()%></textarea>	
			</span>
		</td>
	</tr>
	<tr>
		<td class="td1" colspan="2"><span
			style="margin-left: 30px; font-weight: bold">直接经济损失与间接经济损失：
			</span> 
			<br/>
			<span style="margin-left: 30px; ">
			<textarea rows="5" style="width:90%;" id="loss"></textarea>	
			</span>
		</td>
	</tr>
	<tr>
		<td class="td1" colspan="2"><span
			style="margin-left: 30px; font-weight: bold">原因分析：
			</span> 
			<br/>
			<span style="margin-left: 30px; ">
			<textarea rows="10" style="width:90%;" id="reason"></textarea>	
			</span>
		</td>
	</tr>
	<tr>
		<td class="td1" colspan="2"><span
			style="margin-left: 30px; font-weight: bold">教训及防范措施：
			</span> 
			<br/>
			<span style="margin-left: 30px; ">
			<textarea rows="10" style="width:90%;" id="zhenggai_cuoshi"><%=problem.getProblemZhenggai()%></textarea>	
			</span>
		</td>
	</tr>
	<tr>
		<td class="td1" colspan="2"><span
			style="margin-left: 30px; font-weight: bold">责任分析及处理情况：
			</span> 
			<br/>
			<span style="margin-left: 30px; ">
			<textarea rows="10" style="width:90%;" id="duty"></textarea>	
			</span>
		</td>
	</tr>
	<tr>
		<td class="td1" colspan="2"><span
			style="margin-left: 30px; font-weight: bold">调查小组人员名单：
			</span> 
			<br/>
			<span style="margin-left: 30px; ">
			<textarea rows="5" style="width:90%;" id="team"></textarea>	
			</span>
		</td>
	</tr>
		<tr>
			<td class="td1">
				<span style="margin-left: 30px; font-weight: bold">反馈信息：</span>
				<br />
				<div id="feedbackProcessMethodDiv">
				<%@include file="/shutdown/feedback_process_method.jsp" %>
				</div>				
			</td>
		</tr>
		<tr>
			<td class="td1" colspan="2">
				<span style="margin-left: 30px" class="span1">填报人及时间：</span>
				<span><%=user.getDepartment1Name()%>-<%=user.getDepartment2Name()%>-<%=user.getDepartment3Name()%></span>
				<span><%=user.getName()%></span>
				<span><%=Util.formatDateTime(Calendar.getInstance())%></span>
			</td>
		</tr>
		<tr>
			<td class="td1" align="center" colspan="2">
				<% if(!"postcheckReport".equals(fromPage)) {%>
				<input type="button" value="上报" onclick="convertProblem()" />
				<%} %>
				<input type="button" value="取 消" style="margin-left: 20px" onclick="cancelConvert()" />
			</td>
		</tr>
	</table>
</div>

<script type="text/javascript">
function convertProblem() {	
	var shutdownTypeID = getElementValueByID("shutdownType");
	var problemTitle = getElementValueByID("problemTitle");
	var problemContent = getElementValueByID("problemContent");
	var loss = getElementValueByID("loss");
	var reason = getElementValueByID("reason");
	var zhenggai_cuoshi = getElementValueByID("zhenggai_cuoshi");
	var duty = getElementValueByID("duty");
	var team = getElementValueByID("team");
	
	var startYear = $("#startYear").val();
	var startMonth = $("#startMonth").val();
	var startDay = $("#startDay").val();
	var startHour = $("#startHour").val();
	var startMinute = $("#startMinute").val();
	
	var endYear = $("#endYear").val();
	var endMonth = $("#endMonth").val();
	var endDay = $("#endDay").val();
	var endHour = $("#endHour").val();
	var endMinute = $("#endMinute").val();
	
	
	var processMethod = 0;
	var processRadio3 = document.getElementById('processRadio3');
	if (processRadio3 != null && processRadio3.checked) {
		processMethod = 3;
	} 
	
	if(shutdownTypeID == 0) {
		$.dialog.alert('请选择性质');
		return;
	}
	
	if(problemTitle == null || problemTitle == '' || problemTitle == 'undefined') {
		$.dialog.alert('请填写名称');
		return;
	}
	
	if(reason == null || reason == '' || reason == 'undefined') {
		$.dialog.alert('请填写原因');
		return;
	}	
	
	
	if(zhenggai_cuoshi == null || zhenggai_cuoshi == '' || zhenggai_cuoshi == 'undefined') {
		$.dialog.alert('请填写教训及防范措施');
		return;
	}	
	
	if(duty == null || duty == '' || duty == 'undefined') {
		$.dialog.alert('请填写责任分析及处理情况');
		return;
	}
	
	if(team == null || team == '' || team == 'undefined') {
		$.dialog.alert('请填写调查小组人员名单');
		return;
	}
	
	var escalateZongbu = null;
	if(document.getElementById('escalateZongbu')!=null){
		escalateZongbu = $('#escalateZongbu').attr('checked');
	}
	
	if ($.dialog.confirm('您确定上报此问题为<%=problemName %>报告吗？', function(){
		openWaitingDialog('waitingDialog');
		$.ajax({
			type:'post',
			url:'<%=request.getContextPath()%>/shutdown/convert_problem_process.jsp',
			data:{
				problemType: <%=problemType%>,
				processMethod: processMethod,
				id: <%=problemID%>,
				transferID: <%=(currentTransfer==null?"''":currentTransfer.getId())%>,
				feedback: '',
				shutdownTypeID: shutdownTypeID,
				problemTitle: problemTitle,
				problemContent: problemContent,
				loss: loss,
				reason: reason,
				zhenggai_cuoshi: zhenggai_cuoshi,
				duty: duty,
				team: team,
				startYear: startYear,
				startMonth: startMonth,
				startDay: startDay,
				startHour: startHour,
				startMinute: startMinute,
				endYear: endYear,
				endMonth: endMonth,
				endDay: endDay,
				endHour: endHour,
				endMinute: endMinute,
				escalateZongbu: escalateZongbu
			},
			success:function(data,state,xmlhttp){
				var obj = null;
				try {
					obj = eval('(' + data + ')');
				} catch (e) {
				}
				if (typeof(obj.isSuccess) != 'undefined') {
					closeWaitingDialog('waitingDialog');
					if (obj.isSuccess == 'true') {
						$.dialog('<%=problemName %>报告填报成功！', function() { loadPageSection(
								'<%=request.getContextPath()%>/shutdown/problem_browse.jsp', 
								'mainContentDiv', 
								{
									problemType: <%=problemType%>,
									fromPage: 'feedback'
								}
							);
						});
					} else {
						$.dialog.alert(obj.error);
					}
				}
			}
		});
	}, function(){}));
}

function cancelConvert() {
	$.dialog.confirm('您确定取消此次输出吗？', function() {
		loadPageSection(
				'<%=request.getContextPath()%>/shutdown/problem_feedback.jsp', 
				'mainContentDiv', 
				{
					problemType: <%=problemType%>,
					id:<%=problem.getId()%>, 
					fromPage:'shutdown'
				}, 
			true);
	}, function(){});
}

function refreshMonthEnd(year, month, dayEle){
	var day = $(dayEle).val();
	
	$(dayEle).empty();
	for (var i = 1;i<=28;i++){
		 $(dayEle).append("<option value='"+i+"'>"+i+"</option>"); 
	}
	if(month == 2){
		var maxDay = 28;
		if(isLeap(year)){
			//闰年
			 $(dayEle).append("<option value='"+29+"'>"+29+"</option>"); 
			maxDay = 29;
		}
		if(day>maxDay){
			day = maxDay;
		}

		 $(dayEle).val(day);
	} else if(month == 4 || month == 6 || month == 9 || month == 11){
		//30天
		 $(dayEle).append("<option value='"+29+"'>"+29+"</option>"); 
		 $(dayEle).append("<option value='"+30+"'>"+30+"</option>");
		 
		 if(day == 31 ){
			 day = 30;
		 }
		 $(dayEle).val(day);
	} else {
		//31天
		 $(dayEle).append("<option value='"+29+"'>"+29+"</option>"); 
		 $(dayEle).append("<option value='"+30+"'>"+30+"</option>"); 
		 $(dayEle).append("<option value='"+31+"'>"+31+"</option>"); 
		 $(dayEle).val(day);
	}
}

function refreshPeriodStart(updateMonthEnd){
	var startYear = $("#startYear").val();
	var startMonth = $("#startMonth").val();
	
	if(updateMonthEnd){
		refreshMonthEnd(startYear, startMonth, $("#startDay"));
	}
	
	var startDay = $("#startDay").val();
	var startHour = $("#startHour").val();
	var startMinute = $("#startMinute").val();
	
	var endYear = $("#endYear").val();
	var endMonth = $("#endMonth").val();
	var endDay = $("#endDay").val();
	var endHour = $("#endHour").val();
	var endMinute = $("#endMinute").val();
	
	var minutes = getMinutes(endYear, endMonth, endDay, endHour, endMinute, startYear, startMonth, startDay, startHour, startMinute);

	if(minutes < 0){
		$.dialog.alert("请确保停工结束时间晚于停工开始时间");
		return;
	}
	$("#periodMinutes").val(minutes);
	var period = getPeriod(minutes);

	$("#periodDay").val(period[0]);
	$("#periodHour").val(period[1]);
	$("#periodMinute").val(period[2]);
}

function refreshPeriodEnd(updateMonthEnd){
	var startYear = $("#startYear").val();
	var startMonth = $("#startMonth").val();
	var startDay = $("#startDay").val();
	var startHour = $("#startHour").val();
	var startMinute = $("#startMinute").val();
	
	var endYear = $("#endYear").val();
	var endMonth = $("#endMonth").val();

	if(updateMonthEnd){
		refreshMonthEnd(startYear, startMonth, $("#endDay"));
	}
	
	var endDay = $("#endDay").val();
	var endHour = $("#endHour").val();
	var endMinute = $("#endMinute").val();
	
	var minutes = getMinutes(endYear, endMonth, endDay, endHour, endMinute, startYear, startMonth, startDay, startHour, startMinute);

	$("#periodMinutes").val(minutes);
	var period = getPeriod(minutes);

	$("#periodDay").val(period[0]);
	$("#periodHour").val(period[1]);
	$("#periodMinute").val(period[2]);
}
function chooseProcessMethod(method) {
//empty.
	//convertProblem();
}
</script>
